package com.wsoft.cms.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wsoft.cms.entity.CmsCategoryAuthEntity;
import com.wsoft.cms.manager.CmsCategoryAuthManager;
import com.wsoft.cms.mapper.CmsCategoryAuthMapper;
import com.wsoft.cms.query.cate.auth.CateAuthUpdateQuery;
import com.wsoft.cms.service.ICmsCategoryAuthService;
import com.wsoft.core.service.RedisService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 公告类型授权表 服务实现类
 * </p>
 *
 * @author juiet
 * @since 2024年11月27日 10:39:27
 */
@Service
public class CmsCategoryAuthServiceImpl extends ServiceImpl<CmsCategoryAuthMapper, CmsCategoryAuthEntity> implements ICmsCategoryAuthService {

    @Resource
    private CmsCategoryAuthManager categoryAuthManager;
    @Resource
    private RedisService redisService;

    /**
     * 修改分类权限
     * @param query
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateCategoryAuth(CateAuthUpdateQuery query) {
        categoryAuthManager.delByCategoryId(query.getCategoryId());
        return this.save(query.getCategoryId(),query.getOrgIdList());
    }

    /**
     * 根据 categoryId 获取
     * @param categoryId
     * @return
     */
    @Override
    public List<Long> getByCategoryId(Long categoryId) {
        return categoryAuthManager.getByCategoryId(categoryId);
    }


    // ################################## 私有方法  ##################################
    /**
     *
     * @param categoryId
     * @param orgIdList
     * @return
     */
    private boolean save(Long categoryId,List<Long> orgIdList){
        if(CollUtil.isNotEmpty(orgIdList)){
            List<CmsCategoryAuthEntity> list = new ArrayList<>();
            for (Long orgId : orgIdList) {
                CmsCategoryAuthEntity auth = new CmsCategoryAuthEntity();
                auth.setCategoryId(categoryId);
                auth.setOrgId(orgId);
                list.add(auth);
            }
            return categoryAuthManager.saveBatch(list);
        }
        return false;
    }
}
